<?php

namespace app\stores\controller\admin\marketing;

use app\BaseController;
use app\beautiful\model\mall\GoodsModel;
use app\common\model\DataConfigModel;
use app\common\model\ShopModel;
use app\stores\model\promotion\ActivityModel;
use app\stores\model\promotion\TeamModel;

class Promotion extends BaseController {
    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $status = $this->request->param('status/d',1);
        $name = $this->request->param('name');
        $start_sales_num = $this->request->param('start_sales_num');
        $end_sales_num = $this->request->param('end_sales_num');

        $where['shop_id']=$this->request->shopId;
        $where['status']=$status;
        $where['name']=$name;
        if($start_sales_num!='') $where['start_sales_num']=$start_sales_num;
        if($end_sales_num!='') $where['end_sales_num']=$end_sales_num;
        $order['a.activity_id']='desc';
        $field='a.activity_id,a.title,a.img,a.price,a.commission,a.sales_num,a.quota,a.start_time,a.end_time,a.add_time,g.cover_img,g.name,g.original_price,g.total_value,g.type,g.sell_time_type,g.sell_start_time,g.sell_end_time';
        $return = ActivityModel::list($where,$order,$field,$page,$limit);
        return $this->success($return);
    }
    public function info(){
        $activity_id = $this->request->param('activity_id/d',0);
        $activity = ActivityModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'activity_id'=>$activity_id])->find();
        if(null === $activity) return $this->error('活动不存在');
        $return = ActivityModel::info(['a.activity_id'=>$activity_id],'a.*,g.name,g.original_price,g.cover_img,g.sell_time_type,g.sell_start_time,g.sell_end_time,g.use_limit_detail');
        return $this->success($return);
    }
    /*添加推广活动*/
    public function saveActivity(){
        $activity_id = $this->request->param('activity_id/d',0);
        $goods_id = $this->request->param('goods_id/d',0);
        $title = $this->request->param('title');
        $price = $this->request->param('price');
        $commission = $this->request->param('commission');
        $type = $this->request->param('type/d',1);
        $is_appoint_team = $this->request->param('is_appoint_team/d',0);
        $team_ids = $this->request->param('team_ids');
        $settle_type = $this->request->param('settle_type/d',1);
        $start_time = $this->request->param('start_time');
        $end_time = $this->request->param('end_time');
        $quota = $this->request->param('quota/d',0);
        $is_limit_buy = $this->request->param('is_limit_buy/d',0);
        $can_buy_type = $this->request->param('can_buy_type/d',1);
        $can_buy_num = $this->request->param('can_buy_num/d',1);
        $use_type = $this->request->param('use_type/d',1);
        $use_time_type = $this->request->param('use_time_type/d',1);
        $use_time_num = $this->request->param('use_time_num/d',1);
        $use_start_time = $this->request->param('use_start_time');
        $use_end_time = $this->request->param('use_end_time');
        $img = $this->request->param('img');
        $notice = $this->request->param('notice');
        $status = $this->request->param('status/d',0);

        if($activity_id>0){
            $activity = ActivityModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'activity_id'=>$activity_id])->find();
            if(null === $activity) return $this->error('活动不存在');
        }else{
            $activity= new ActivityModel();
            $activity->shop_id=$this->request->shopId;
        }

        $goods = GoodsModel::where(['goods_id'=>$goods_id,'shop_id'=>$this->request->shopId,'is_delete'=>0])->find();
        if(null === $goods) return $this->error('商品不存在');

        //推广佣金不能大于活动价格-活动价格的百分之2
        $config = DataConfigModel::where(['is_lock'=>1])->find();
        $shop = ShopModel::where(['shop_id'=>$this->request->shopId,'is_lock'=>1])->find();
        if($config && $shop){
            $max_commission = $price - $price * ($config['lock_ratio']/100);
            if($max_commission > $max_commission) return $this->error('推广佣金不能大于'.$max_commission);
        }

        if($team_ids) $team_ids = implode(',',$team_ids);

        if($type==2) $is_appoint_team=0;
        $activity->goods_id = $goods_id;
        $activity->title = $title;
        $activity->price = $price;
        $activity->commission = $commission;
        $activity->type = $type;
        $activity->is_appoint_team = $is_appoint_team;
        $activity->team_ids = $is_appoint_team>0?$team_ids:null;
        $activity->settle_type = $settle_type;
        $activity->start_time = $start_time;
        $activity->end_time = $end_time;
        $activity->quota = $quota;
        $activity->is_limit_buy = $is_limit_buy;
        $activity->can_buy_type = $can_buy_type;
        $activity->can_buy_num = $can_buy_num;
        $activity->use_type = $use_type;
        $activity->use_time_type = $use_time_type;
        $activity->use_time_num = $use_time_num;
        $activity->use_start_time = $use_start_time;
        $activity->use_end_time = $use_end_time;
        $activity->img = $img;
        $activity->notice = $notice;
        $activity->status = $status;
        $activity->edit_time = date('Y-m-d H:i:s');
        $activity->save();

        return $this->success();

    }
    //获取团队
    public function getTeam(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $name = $this->request->param('name');
        $where['is_effect'] = 1;
        $where['name'] = $name;
        $order=['add_time'=>'desc'];
        $return = TeamModel::list($where,$order,$page,$limit);
        return $this->success($return);
    }
    //上下架
    public function saveStatus(){
        $activity_id = $this->request->param('activity_id/d',0);
        $status = $this->request->param('status/d',1);
        $activity = ActivityModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'activity_id'=>$activity_id])->find();
        if(null === $activity) return $this->error('活动不存在');
        $activity->status = $status;
        $activity->save();
        return $this->success('操作成功');
    }

}

